package com.guigu.buy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.buy.dto.Product_Supplier;
import com.guigu.buy.pojo.Product;
import com.guigu.buy.mapper.ProductMapper;
import com.guigu.buy.pojo.ProductType;
import com.guigu.buy.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.buy.service.IProductTypeService;
import com.guigu.buy.service.IStockService;
import com.guigu.buy.vo.ProductVo;
import com.guigu.buy.vo.ProductVo2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author com.guigu
 * @since 2024-05-15
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IProductTypeService iProductTypeService;
    @Autowired
    private IStockService stockService;
    @Override
    public PageInfo<Product> queryAll(int pageNo, int pageSize, ProductVo vo) {
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        if(!StringUtils.isEmpty(vo.getProductName())){
            wrapper.eq("product_name",vo.getProductName());
        }if(!StringUtils.isEmpty(vo.getProductType1()) &&vo.getProductType1()!=0){
            wrapper.eq("type",vo.getProductType1() );
        }if(!StringUtils.isEmpty(vo.getProductType2()) &&vo.getProductType2()!=0){
            wrapper.eq("type",vo.getProductType2());
        }
        List<Product> list =this.list(wrapper);
        PageHelper.startPage(pageSize,pageNo);
        PageInfo<Product> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Product_Supplier queryOne(int id) {
        return productMapper.getOne(id);
    }
    /**
     * 主页文本框 模糊查询商品
     * @return
     */
    @Override
    public List<Product> select(String productName) {
        List<Product> list=productMapper.selectProduct(productName);
        return list;
    }

    //促销商品 所有商品卖的最少的前五个
    @Override
    public List<Product> listClothes() {
        List<Product> list=productMapper.listClothes();
        List<Product> productList=new ArrayList<>();
        for (int i=0;i<list.size();i++){
            QueryWrapper<Product> wrapper=new QueryWrapper<>();
            wrapper.eq("id",list.get(i).getProductId());
             //productList= this.list(wrapper);
             productList.add(this.list(wrapper).get(0));
            System.out.println("数据"+productList);
        }
        return productList;
    }

    @Override
    public List<Product> listSortProduct(int id) {
        QueryWrapper<ProductType> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Product> list=new ArrayList<>();
        List<ProductType> typeList=iProductTypeService.list(wrapper);
        for (int i=0;i<typeList.size();i++){
            QueryWrapper<Product> wrapper1=new QueryWrapper<>();
            wrapper1.eq("type",typeList.get(i).getId());
            for(int j=0;j<this.list(wrapper1).size();j++){
                list.add(this.list(wrapper1).get(j));
            }
        }
        return list;
    }
    //主页查询推荐商品 只查今年的商品
    @Override
    public List<Product> listRecommended() {
        List<Product> list=productMapper.listRecommended();
        return list;
    }

    @Override
    public PageInfo<Product> selectAll(int pageNo, int pageSize, ProductVo2 vo) {
        QueryWrapper<Product> wrapper=new QueryWrapper<>();
        wrapper.eq("status","0");
        if(!StringUtils.isEmpty(vo.getProductName())){
            wrapper.like("product_name",vo.getProductName());
        }
        if(!StringUtils.isEmpty(vo.getProductStatus()) && vo.getProductStatus()!="0"){
            wrapper.eq("product_status",vo.getProductStatus());
        }

        PageHelper.startPage(pageSize,pageNo);
        List<Product> list =this.list(wrapper);
        PageInfo<Product> pageInfo=new PageInfo<>(list);
        return pageInfo;
    }
    //删除商品
    @Override
    public Boolean delProduct(int id) {
        Product product=new Product();
        product.setStatus("1");
        product.setId(id);
        Boolean fale= this.updateById(product);
        return fale;
    }

    @Override
    public String getMaxDesignId() {
        Product product=productMapper.queryMaxModule();
        if (product==null){
            return null;
        }
        String productId = product.getProductId();
        return productId;
    }

    @Override
    public Product getProductById(Integer id) {
        return (Product) productMapper.findById(id).orElse(null);
    }

    //根据商品名字查询

    @Override
    public PageInfo<Product> listPage(Product vo, int pageNo, int pageSize) {
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(vo.getProductName()) && vo.getProductName()!="") {
            wrapper.eq("product_name", vo.getProductName());
        }

        List<Product> list = this.list(wrapper);
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<Product> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //点击供应 查中间表和商品

    @Override
    public List<Product> buyId(int id) {
        List<Product> list=productMapper.buysupplierAll();
        return list;
    }

}
